package cn.edu.ncu.housinghub.controller.admin;

import cn.edu.ncu.housinghub.constant.ErrorCodeConstant;
import cn.edu.ncu.housinghub.constant.SystemConstant;
import cn.edu.ncu.housinghub.exception.BusinessException;
import cn.edu.ncu.housinghub.service.AdminAgentService;
import cn.edu.ncu.housinghub.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 中介资质审核、权限管理
 */
@RestController
@RequestMapping("/admin/agent")
@Slf4j
public class AdminAgentController {
    @Autowired
    private AdminAgentService adminAgentService;

    /**
     * 查看所有中介资质申请
     * @return
     */
    @GetMapping("/list")
    public Result list() {
        try {
            log.info("开始获取中介申请列表...");
            return Result.success(adminAgentService.findAllApply());
        } catch (BusinessException e) {
            log.error("获取中介申请列表失败: {}", e.getMessage(), e);
            return Result.error(e.getErrorCode().getCode(), e.getMessage());
        } catch (Exception e) {
            log.error("获取中介申请列表发生系统错误", e);
            return Result.error(ErrorCodeConstant.SYSTEM_ERROR.getCode(), ErrorCodeConstant.SYSTEM_ERROR.getMessage());
        }
    }

    /**
     * 拒绝申请
     * @return
     */
    @PostMapping("/refuse")
    public Result refuse(@RequestBody Map<String, Object> requestBody) {
        Long userId = requestBody.get("userId") != null ? Long.valueOf(requestBody.get("userId").toString()) : null;
        String refuseReason = (String) requestBody.get("refuseReason");
        try {
            if (userId == null) {
                throw new BusinessException(ErrorCodeConstant.PARAM_VALIDATION_FAILED, "用户ID不能为空");
            }
            adminAgentService.modifyApplyStatusById(userId,
                SystemConstant.AGENT_VERIFY_STATUS_REJECTED.getIntegerValue(), refuseReason);
            return Result.success("中介申请拒绝成功");
        } catch (BusinessException e) {
            log.error("拒绝中介申请失败: userId={}, reason={}, error={}", userId, refuseReason, e.getMessage(), e);
            return Result.error(e.getErrorCode().getCode(), e.getMessage());
        } catch (Exception e) {
            log.error("拒绝中介申请发生系统错误: userId={}", userId, e);
            return Result.error(ErrorCodeConstant.SYSTEM_ERROR.getCode(), ErrorCodeConstant.SYSTEM_ERROR.getMessage());
        }
    }

    /**
     * 同意申请
     * @return
     */
    @GetMapping("/agree/{userId}")
    public Result agree(@PathVariable Long userId) {
        try {
            if (userId == null) {
                throw new BusinessException(ErrorCodeConstant.PARAM_VALIDATION_FAILED, "用户ID不能为空");
            }
            adminAgentService.modifyApplyStatusById(userId,
                SystemConstant.AGENT_VERIFY_STATUS_APPROVED.getIntegerValue(), null);
            return Result.success("中介申请审核通过");
        } catch (BusinessException e) {
            log.error("同意中介申请失败: userId={}, error={}", userId, e.getMessage(), e);
            return Result.error(e.getErrorCode().getCode(), e.getMessage());
        } catch (Exception e) {
            log.error("同意中介申请发生系统错误: userId={}", userId, e);
            return Result.error(ErrorCodeConstant.SYSTEM_ERROR.getCode(), ErrorCodeConstant.SYSTEM_ERROR.getMessage());
        }
    }

    /**
     * 测试依赖注入和数据库连接
     * @return
     */
    @GetMapping("/test")
    public Result test() {
        try {
            log.info("开始测试依赖注入和数据库连接...");

            // 测试service注入
            if (adminAgentService == null) {
                return Result.error(ErrorCodeConstant.SYSTEM_ERROR.getCode(), "AdminAgentService 注入失败");
            }

            // 测试数据库查询
            adminAgentService.findAllApply();

            return Result.success("依赖注入和数据库连接测试成功");
        } catch (Exception e) {
            log.error("测试失败", e);
            return Result.error(ErrorCodeConstant.SYSTEM_ERROR.getCode(), "测试失败: " + e.getMessage());
        }
    }
}
